<template>
  <el-tree
    v-if="isShowTree"
    :data="treeData"
    ref="tree"
    :props="defaultProps"
    node-key="id"
    :current-node-key="currentKey"
    @current-change="handleNodeClick"
    :default-expanded-keys="expandKey"
    highlight-current
    :filter-node-method="filterNode"
  >
    <div class="custom-tree-node" slot-scope="{ node, data }" :data-level="node.level">
      <div>
        <!-- 没有子元素时显示的图标 (即是最后一层) -->
        <i
          v-if="!data.children || data.id == '0'"
          :class="defaultOpen"
          style="color: #448ac4"
        >
        </i>
        <template v-if="showIcon">
          <span v-if="!data.children || data.id == '0'">
            <i class="el-icon-folder" style="color: #4da802"></i>
          </span>
        </template>
        <!-- 有子元素显示的图标 -->
        <span v-if="data.children && data.id != '0'">
          <i
            :class="node.expanded ? defaultOpen : defaultClose"
            style="color: #448ac4"
          >
          </i>
          <!-- 这里文件打开和关闭图标的切换  -->
          <template v-if="showIcon">
            <template v-if="data.parentId == -1">
              <img
                v-if="$route.path.indexOf('dynamic') > -1"
                src="@/assets/image/work2.jpg"
                alt="工作区图标"
              />
              <img
                v-if="$route.path.indexOf('library') > -1"
                src="@/assets/image/work1.jpg"
                alt="文档库图标"
              />
            </template>
            <i
              v-else
              :class="'el-icon-folder-opened'"
              style="color: #4da802"
            ></i>
          </template>
        </span>
        <!-- 名称 -->
        <span class="label" :title="node.label">{{ node.label }}</span>
      </div>
    </div>
  </el-tree>
</template>

<script>
import { getTreeNode } from "@/utils";

export default {
  props: {
    treeData: {
      type: Array,
      default: () => [],
    },
    folderName: {
      type: String,
      default: "",
    },
    showIcon: true,
  },
  data() {
    return {
      defaultOpen: "el-icon-remove-outline",
      defaultClose: "el-icon-circle-plus-outline",
      // 数据列表
      defaultProps: {
        children: "children",
        label: "label",
      },
      currentKey: 0,
      hasOpen: false,
      expandAll: true,
      expandKey: [],
    };
  },
  computed: {
    isShowTree() {
      return this.treeData.length && this.currentKey;
    },
  },
  watch: {
    folderName(val) {
      this.$refs.tree.filter(val);
    },
    treeData: {
      //深度监听，可监听到对象、数组的变化
      handler(val) {
        if (val) {
          let treeOption = JSON.parse(sessionStorage.getItem(
            "tree" + this.$route.path.split("/").pop()
          ));
          // console.log(treeOption)
          this.currentKey =
            treeOption != "undefined" && treeOption != null
              ? Number(treeOption.currentKey)
              : val[0].id;
          this.expandAll = treeOption != "undefined" && treeOption != null
              ?true:false
          this.expandKey = treeOption != "undefined" && treeOption != null
              ?treeOption.expandKey:[]
          let data = getTreeNode(val, this.currentKey)||val[0];
          // if(!data){
          //   data = val[0]
          // }
          if (data) {
            setTimeout(()=>{this.handleNodeClick(data)},1000);
          }
        }
      },
      deep: true, //true 深度监听
    },
  },
  methods: {
    handleNodeClick(data, node) {
      if (typeof data != "object") {
        data = getTreeNode(this.treeData, Number(data));
        this.$refs.tree.setCurrentKey(data.id);
      }
      this.currentKey = data.id;
      if(this.expandKey.indexOf(data.id)==-1){
        this.expandKey.push(data.id)
      }else{
        this.expandKey.splice(this.expandKey.indexOf(data.id),1)
      }
      sessionStorage.setItem(
        "tree" + this.$route.path.split("/").pop(),
        JSON.stringify({currentKey:this.currentKey,expandKey:this.expandKey})
      );
      this.$emit("handleNodeClick", data, node);
    },
    filterNode(value, data) {
      if (!value) return true;
      if (
        data.label.toLowerCase().indexOf(value) !== -1 ||
        data.label.toUpperCase().indexOf(value) !== -1
      ) {
        return true;
      }
    },
  },
};
</script>

<style>
img {
  vertical-align: sub;
}
.el-tree {
  width: 100%;
  font-size: 14px;
}
.is-current > .el-tree-node__content {
  color: #1890ff;
}
/* // 将每一行的设置为相对定位 方便后面before after 使用绝对定位来固定位置 */
.el-tree-node {
  position: relative;
  padding-left: 10px;
  box-sizing: border-box;
}
.custom-tree-node .label {
  display: inline-block;
  /* overflow: hidden; */
  white-space: nowrap;
  text-overflow: ellipsis;
  vertical-align: middle;
}
/* // 子集像右偏移 给数线留出距离 */
.el-tree-node__children {
  padding-left: 10px;
}
/* //这是竖线 */
.el-tree-node :last-child:before {
  height: 25px;
}
.el-tree > .el-tree-node:before {
  border-left: none;
}
.el-tree > .el-tree-node:after {
  border-top: none;
}
/* //这自定义的线 的公共部分 */
.el-tree-node:before,
.el-tree-node:after {
  content: "";
  left: -4px;
  position: absolute;
  right: auto;
  border-width: 1px;
}
.tree :first-child .el-tree-node:before {
  border-left: none;
}
/* // 竖线 */
.el-tree-node:before {
  border-left: 1px solid #606266;
  bottom: 0px;
  height: 100%;
  top: -10px;
  width: 1px;
}
/* //横线 */
.el-tree-node:after {
  /* //border-top: 1px solid #e3e3e3; */
  border-top: 1px solid #606266;
  height: 20px;
  top: 14px;
  width: 15px;
}
.el-tree-node__expand-icon.is-leaf {
  width: 8px;
}
/* //去掉elementui自带的展开按钮  一个向下的按钮,打开时向右 */
.el-tree-node__content > .el-tree-node__expand-icon {
  display: none !important;
}
/* //每一行的高度 */
.el-tree-node__content {
  padding-left: 0 !important;
  line-height: 30px;
  height: 30px;
}
</style>
